home *** CD-ROM | disk | FTP | other *** search
- /******************************************************************************
- *
- * NSSDC/CDF Header file for dynamic arrays of pointers.
- *
- * Version 2.0, 27-Feb-92, ST Systems (STX)
- *
- * Modification history:
- *
- * V1.0 24-Jan-91, D Grogan/H Leckner Original version (for CDF V2.0).
- * V1.1 27-Feb-92, J Love Modified for IBM-PC port. CDF V2.2.
- * H Leckner
- *
- ******************************************************************************/
-
- /*
- Use LARGE model on PC.
-
- See the 3 key macros, used in this order:
- 1) PTR_LIST_MALLOC initializes a list of n pointers to any type.
- For example,
- char **myPagetext;
- long nLines=60;
- myPageText = PTR_LIST_MALLOC( nLines, char);
-
- 2) FIXARRAY_MALLOC establishes a fixed number of items of any type
- for the initialized pointer-list to point to.
- long lineSize=81;
- FIXARRAY_MALLOC( myPageText, nLines, char, lineSize);
- Test the first item for success: if (myPageText[0] != NULL) OK;
- If you need a varying length for each item pointed-to,
- then just malloc it yourself, and don't use FIXARRAY_MALLOC:
- for(k=0; k<nLines; k++) myPageText[k] = malloc(need[k]);
-
- 3) PTR_LIST_FREE undoes what PTR_LIST_MALLOC does, leaving no dangling
- pointers.
- PTR_LIST_FREE( myTextPage, nLines);
- */
-
- /* This macro will produce an array[np] of pointers to type */
- /* You must test the return pointer yourself for NULL value */
- #define PTR_LIST_MALLOC(np,type) (type **) malloc( np * sizeof( type *) )
-
- /* This macro will allocate nElem of type to each p[0..np-1] */
- /* It tests each malloc for NULL */
- #define FIXARRAY_MALLOC(p,np,type,nElem) {long j;/*, err; err=0;*/ \
- for (j=0; j<np; j++) { \
- p[j] = (type *) malloc( nElem * sizeof(type) ); \
- if (p[j] == NULL) { long k; \
- for (k=0; k<j; k++) { \
- free( p[k]);/* free & return */ \
- p[k] = NULL;} \
- break; \
- } \
- } } /* end FIXARRAY_MALLOC */
-
- /* This macro frees the PTR list, p[0..np-1], leaving no danglers. */
- /* And then it frees p */
- #define PTR_LIST_FREE(p,np) {long j; \
- for (j=0; j<np; j++) { \
- free( p[j]); p[j] = NULL; } \
- free( p); p = NULL;}
- /* end PTR_LIST_FREE */
-
-